package com.tech.tool.service.impl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.tech.tool.dataobject.ApiInfo;
import com.tech.tool.dto.swagger.*;
import com.tech.tool.service.AnalyzeSwaggerService;
import com.tech.tool.utils.HttpUtil;
import com.tech.tool.utils.Json2OtherUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class AnalyzeSwaggerServiceImpl implements AnalyzeSwaggerService {

    @Override
    public List<ApiInfo> getApiInfoList(String url) {

        String response = HttpUtil.get(url, "", "");
        SwaggerDTO swaggerDTO = new Gson().fromJson(response, SwaggerDTO.class);
        JsonObject paths = swaggerDTO.getPaths();
        JsonObject definitions = swaggerDTO.getDefinitions();
        Set<Map.Entry<String, JsonElement>> pathsSet = Json2OtherUtil.json2Set(paths);
        pathsSet.stream().forEach(s -> {
            String api = s.getKey();
            JsonElement jsonElement = s.getValue();
            String method = Json2OtherUtil.elementGetKey(jsonElement);
            SwaggerPathPostDTO swaggerPathPostDTO = Json2OtherUtil.json2DTO(jsonElement.getAsJsonObject(), SwaggerPathPostDTO.class);

            SwaggerPathPostParametersDTO swaggerPathPostParametersDTO = swaggerPathPostDTO.getParameters().get(0);
            SwaggerSchemaDTO swaggerSchemaDTO = swaggerPathPostParametersDTO.getSchema();
            String ref = swaggerSchemaDTO.getRef();
            String definitionName = ref.replace("#/definitions/", "");
            SwaggerDefinitionDTO swaggerDefinitionDTO = Json2OtherUtil.jsonName2DTO(definitions, definitionName, SwaggerDefinitionDTO.class);
            JsonObject properties = swaggerDefinitionDTO.getProperties();
            Set<Map.Entry<String, JsonElement>> propertiesSet = Json2OtherUtil.json2Set(properties);
            Iterator<Map.Entry<String, JsonElement>> propertiesIterator = propertiesSet.iterator();
//            Gson gson = new GsonBuilder().create();
            Gson gson = new Gson();
            while (propertiesIterator.hasNext()) {
                Map<String, Object> mapA = new HashMap<String, Object>();
                Map.Entry<String, JsonElement> propertiesChild = propertiesIterator.next();
                String key = propertiesChild.getKey();
                Object o = propertiesChild.getValue();


            }

            log.info("a");
        });




        return null;
    }
}
